package com.cebbank.sz.cebtemplate.utils;

import java.util.*;

/**
*@Description: 抽签类
*@Param: 
*@return: 
*@Author: Jaco
*@date: 2020/3/14
*/


public class RandomUtils {

//    设置药店限制数量
    public static Map<Integer ,Integer> getLimitMap(int pharmacyNum,int perPharmacyMaxMaskNum) {
        Map<Integer ,Integer> limitMap=new HashMap<>();
        Random r = new Random();
        for (int i = 0; i <pharmacyNum ; i++) {
            int result = r.nextInt(perPharmacyMaxMaskNum);
            limitMap.put(i,result);
        }
        return limitMap;
    }

    public static void main(String[] args) {
//        总登记人数
        int allSize = 600000;
//        总发放口罩份数
        int awardSize = 10000;
//        药店数量
        int pharmacyNum=200;
//        每家药店最大发放数量
        int perPharmacyMaxMarskNum=200;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入中奖人数:");
            int i1 =  sc.nextInt(); ;
            awardSize=i1;
            System.out.println("请输入药店数量:");
            int i2 =  sc.nextInt();
            pharmacyNum=i2;
            System.out.println("请输入每个药店最大发放数量:");
            int i3 =  sc.nextInt();
            perPharmacyMaxMarskNum=i3;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发生异常程序按照默认参数运行！");
//            return ;
        }

//         获得药店发放上限列表
        Map<Integer, Integer> limitMap = getLimitMap(pharmacyNum,perPharmacyMaxMarskNum);
        List<CustomerPharmacy> resultList;
        long start = System.currentTimeMillis();
//        初始化抽奖列表
        List<CustomerPharmacy> customerPharmacyList=initPhamacy(pharmacyNum,allSize);
        long start2 = System.currentTimeMillis();
        System.out.println("初始化用户预订药店列表花了："+(start2-start)+"ms!");
//        根据药店容量表过滤超过数量的情况
        List<CustomerPharmacy> filterPharmacyList=filterPhamacy(customerPharmacyList,limitMap);
        long end1 = System.currentTimeMillis();
        System.out.println("过滤超过药店数量的投放花了："+(end1-start2)+"ms!");
        if (filterPharmacyList.size()>awardSize){
            System.out.println("shuffle!");
            Collections.shuffle(filterPharmacyList);
            resultList=filterPharmacyList.subList(0,awardSize);
        }else {
            resultList=filterPharmacyList;
        }
        long end2=System.currentTimeMillis();
        System.out.println("抽奖花了："+(end1-start2)+"ms!");
        for (int i = 0; i < resultList.size(); i++) {
            String strResult = String.format("第%s个中奖编号为：%s", i + 1, resultList.get(i).getCustomerId());
            System.out.println(strResult);
        }

    }

//    public static void main(String[] args) {
//        int allSize = 600000;
//        int awardSize = 20000;
//        List<Integer> sourceList=new ArrayList<>();
//        for (int i = 0; i < allSize; i++) {
//            sourceList.add(i);
//        }
//        long end1=System.currentTimeMillis();
//        Collections.shuffle(sourceList);
//        long end2=System.currentTimeMillis();
//        List<Integer> resultList = sourceList.subList(0, awardSize);
//        for (int i = 0; i < resultList.size(); i++) {
//            String strResult = String.format("第%s个中奖编号为：%s", i + 1, resultList.get(i));
//            System.out.println(strResult);
//        }
//        long end3 = System.currentTimeMillis();
//        long delta = end2 - end1;
//        System.out.println("抽奖花费" + delta + "ms!");
//    }

//    根据容量表来初步按每家店容量来摇号
    private static List<CustomerPharmacy> filterPhamacy(List<CustomerPharmacy> customerPharmacyList, Map<Integer, Integer> limitMap) {
        Map<Integer,List<CustomerPharmacy>> resultMap=new HashMap<>();
        List<CustomerPharmacy> resultList=new ArrayList<>();
//        转成按照门店划分的列表
        for (int i = 0; i < customerPharmacyList.size(); i++) {
            CustomerPharmacy customerPharmacy=customerPharmacyList.get(i);
            int pharmacy=customerPharmacy.getPharmacyCode();
            if (resultMap.containsKey(pharmacy)){
                List<CustomerPharmacy> tempList=resultMap.get(pharmacy);
                tempList.add(customerPharmacy);
            }else {
                List<CustomerPharmacy> tempList= new ArrayList<>();
                tempList.add(customerPharmacy);
                resultMap.put(pharmacy,tempList);
            }
        }
//        按照门店进行限量裁剪
        for(Integer index : resultMap.keySet()){
            List<CustomerPharmacy> customerPharmacies=resultMap.get(index);
            int limit=limitMap.get(index);
            if (limit<customerPharmacies.size()){
                Collections.shuffle(customerPharmacies);
                List<CustomerPharmacy> tempList=customerPharmacies.subList(0,limit);
                resultList.addAll(tempList);
            }else {
                resultList.addAll(customerPharmacies);
            }
        }
        return  resultList;
    }

    //        初始化抽奖列表
    public static List<CustomerPharmacy> initPhamacy(int pharmacyNum,int allSize){
        List<CustomerPharmacy> sourceList = new ArrayList<>(pharmacyNum);
        Random r = new Random();
        for (int i = 0; i < allSize; i++) {
            int result = r.nextInt(pharmacyNum);
            String uuid=new Integer(i).toString();
            CustomerPharmacy customerPharmacy=new CustomerPharmacy(result,uuid);
            sourceList.add(customerPharmacy);
        }
        return sourceList;
    }



}
